/**
 * Copyright 2019-2024 覃海林(qinhaisenlin@163.com).
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */ 

package com.qinhailin.portal.echart.ctrl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.jfinal.core.Controller;
import com.jfinal.core.Path;
import com.mysql.jdbc.StringUtils;
import com.qinhailin.portal.echart.service.EchartService;
import com.qinhailin.portal.echart.vo.Chart;
import com.qinhailin.portal.echart.vo.EchartData;
import org.jsoup.internal.StringUtil;

/**
 * Echart图表统计
 * @author QinHaiLin
 * @date 2018年11月28日  
 */
@Path("/portal/echart")
public class EchartController extends Controller {

	private EchartService service=EchartService.me;
	
	/**
	 * echart图表统计实例
	 */
	public void index() {
		render("index.html");
	}
	
	/**
	 * 图表统计业务数据接口
	 */
	public void queryData() {
		List<EchartData> list=new ArrayList<>();
		EchartData echartDataResult =new EchartData();
		List<Map<String,Object>> series=new ArrayList<>();
		//TODO 根据具体业务给下面的变量赋值即可
		String[] legend=new String[]{"女生","男生"};//统计实例
		String title="ECharts入门示例";
		String subtitle="部门人数统计";
		String xAxis="x";		//sql中的字段名
		String yAxis="y";		//sql中的字段名
		String chartType="bar";	//bar,line,pie
		String xName="部门";		
		String yName="人数（人）";		
		String tooltipText="";
		
		for(int i=0;i<2;i++){
			String seriesName= legend[i];
			String sql="select b.org_name x ,count(*) y from sys_user a,sys_org b where a.org_id=b.id and a.sex=? group by b.org_name";//统计部门人数sql
			Object[] params=new Object[]{i};
			Chart data =new Chart(title, subtitle, xAxis, yAxis, chartType, xName, yName, seriesName, tooltipText, sql, params);
			echartDataResult=service.getEchartData(data);
			series.add(echartDataResult.getSeries().get(0));
		}

		echartDataResult.setLegend_x("left");
		echartDataResult.setLegend_y("top");
		echartDataResult.setLegend(legend);
		echartDataResult.setSeries(series);
		list.add(echartDataResult);
		renderJson(list);
	}

	/**
	 * 查询体温统计图
	 * */
	public void queryTemperature(){
		String devideid = getPara("devideid");
		String idcard = getPara("idcard");
		if(StringUtils.isNullOrEmpty(devideid)||StringUtils.isNullOrEmpty(idcard)){
			renderJson();
		}
		List<EchartData> list=new ArrayList<>();
		EchartData echartDataResult =new EchartData();
		List<Map<String,Object>> series=new ArrayList<>();
		//TODO 根据具体业务给下面的变量赋值即可
		String title="体温统计";
		String xAxis="x";		//sql中的字段名
		String yAxis="y";		//sql中的字段名
		String chartType="line";	//bar,line,pie
		String xName="时间";
		String yName="温度";
		String tooltipText="";
		String sql="select DATE_FORMAT(a.inputtime,\"%d号%h点%i\") as x,a.bodytemperature as y from i_body_temperature a where  a.devideid = ? and a.idcard = ?  order by inputtime limit 5";
		Object[] params=new Object[]{devideid,idcard};
		Chart data =new Chart(title, "", xAxis, yAxis, chartType, xName, yName, yName, tooltipText, sql, params);
		data.setMarkLineNum(37.5);
		echartDataResult=service.getEchartData(data);
		series.add(echartDataResult.getSeries().get(0));
		echartDataResult.setLegend_x("left");
		echartDataResult.setLegend_y("top");
		echartDataResult.setSeries(series);
		list.add(echartDataResult);
		renderJson(list);
	}

	/**
	 * 查询血氧统计图
	 * */
	public void queryOxygen(){
		String devideid = getPara("devideid");
		String idcard = getPara("idcard");
		if(StringUtils.isNullOrEmpty(devideid)||StringUtils.isNullOrEmpty(idcard)){
			renderJson();
		}
		List<EchartData> list=new ArrayList<>();
		EchartData echartDataResult =new EchartData();
		List<Map<String,Object>> series=new ArrayList<>();
		//TODO 根据具体业务给下面的变量赋值即可
		String title="血氧统计";
		String xAxis="x";		//sql中的字段名
		String yAxis="y";		//sql中的字段名
		String chartType="line";	//bar,line,pie
		String xName="时间";
		String yName="饱和度";
		String tooltipText="";
		String sql="select DATE_FORMAT(a.inputtime,\"%d号%h点%i\") as x,a.bloodoxygen as y from i_blood_oxygen a where  a.devideid = ? and a.idcard = ?  order by inputtime limit 5";
		Object[] params=new Object[]{devideid,idcard};
		Chart data =new Chart(title, "", xAxis, yAxis, chartType, xName, yName, yName, tooltipText, sql, params);
		data.setMarkLineNum(80.5);
		echartDataResult=service.getEchartData(data);
		series.add(echartDataResult.getSeries().get(0));
		echartDataResult.setLegend_x("left");
		echartDataResult.setLegend_y("top");
		echartDataResult.setSeries(series);
		list.add(echartDataResult);
		renderJson(list);
	}

	/**
	 * 查询血压统计图
	 * */
	public void queryBloodpressure(){
		String devideid = getPara("devideid");
		String idcard = getPara("idcard");
		if(StringUtils.isNullOrEmpty(devideid)||StringUtils.isNullOrEmpty(idcard)){
			renderJson();
		}
		List<EchartData> list=new ArrayList<>();
		EchartData echartDataResult =new EchartData();
		EchartData echartDataResult2 =new EchartData();
		List<Map<String,Object>> series=new ArrayList<>();
		//TODO 根据具体业务给下面的变量赋值即可
		String title="血压统计";
		String xAxis="x";		//sql中的字段名
		String yAxis="y";		//sql中的字段名
		String chartType="line";	//bar,line,pie
		String xName="时间";
		String yName2="收缩压";
		String yName="舒张压";
		String tooltipText="";
		String sql="select DATE_FORMAT(a.inputtime,\"%d号%h点%i\") as x,a.systolicpressure as y from i_blood_pressure a where  a.devideid = ? and a.idcard = ?  order by inputtime limit 5";
		String sql2="select DATE_FORMAT(a.inputtime,\"%d号%h点%i\") as x,a.diastolicpressure as y from i_blood_pressure a where  a.devideid = ? and a.idcard = ?  order by inputtime limit 5";
		Object[] params=new Object[]{devideid,idcard};
		Chart data =new Chart(title, "", xAxis, yAxis, chartType, xName, yName, yName, tooltipText, sql, params);
		Chart data2 =new Chart(title, "", xAxis, yAxis, chartType, xName, yName, yName2, tooltipText, sql2, params);
		echartDataResult=service.getEchartData(data);
		series.add(echartDataResult.getSeries().get(0));
		echartDataResult.setLegend_x("left");
		echartDataResult.setLegend_y("top");
		echartDataResult.setSeries(series);

		echartDataResult2=service.getEchartData(data2);
		series.add(echartDataResult2.getSeries().get(0));
		echartDataResult2.setLegend_x("left");
		echartDataResult2.setLegend_y("top");
		echartDataResult2.setSeries(series);
		list.add(echartDataResult);
		list.add(echartDataResult2);
		renderJson(list);
	}

	/**
	 * 统计异常总体信息
	 * */
	public void queryCountWarn(){
		String devideid = getPara("devideid");
		String idcard = getPara("idcard");
		if(StringUtils.isNullOrEmpty(devideid)||StringUtils.isNullOrEmpty(idcard)){
			renderJson();
		}
		List<EchartData> list=new ArrayList<>();
		EchartData echartDataResult =new EchartData();
		List<Map<String,Object>> series=new ArrayList<>();
		String title="预警数据占比";
		String xAxis="x";		//sql中的字段名
		String yAxis="y";		//sql中的字段名
		String chartType="pie";	//bar,line,pie
		String xName="";
		String yName="";
		String tooltipText="";
		String sql="select  iwl.warntype x,count(1) y from i_warning_list iwl where  iwl.devideid = ? and iwl.idcard = ?  group by iwl.warntype";
		Object[] params=new Object[]{devideid,idcard};
		Chart data =new Chart(title, "", xAxis, yAxis, chartType, xName, yName, yName, tooltipText, sql, params);
		echartDataResult=service.getEchartData(data);
		series.add(echartDataResult.getSeries().get(0));
		echartDataResult.setLegend_x("left");
		echartDataResult.setLegend_y("top");
		echartDataResult.setSeries(series);
		list.add(echartDataResult);
		renderJson(list);
	}
	
}
